import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by natalia on 10/1/14.
 */
public class Game {
    Deck deck;
    ArrayList<Hand> hands = new ArrayList<Hand>();

    Game() {
        deck = new Deck();
    }

    public void addHand(Hand hand) {
        hands.add(hand);
        handOut(hand);
    }


    private void handOut(Hand hand) {
        for( int i = 0; i < 7; i++) {
            hand.addCard(deck.getCardsInDeck().remove(0));
        }
        Collections.sort(hand.getHandCards());
    }

    public static Combination findCombination(Hand hand){
        Combination combination = Hand.isRoyalFlush(hand.getHandCards());
        if(combination != null){
            return combination;
        } else {
            combination = Hand.isStraightFlush(hand.getHandCards());
            if(combination != null){
                return combination;
            } else {
                combination = Hand.isFourOfALevel(hand.getHandCards());
                if(combination != null){
                    return combination;
                } else  {
                   combination = Hand.pairs(hand.getHandCards());
					if(combination != null && combination.getLevelOfCard() == Combination.LEVELS.FULL_HOUSE) {
						return combination;
					} else {
						combination = Hand.checkIfFlush(hand.getHandCards());
						if(combination != null) {
							return combination;
						} else {
							combination = Hand.isStraight(hand.getHandCards());
							if(combination != null) {
								return combination;
							} else {
								combination = Hand.pairs(hand.getHandCards());
								if(combination != null) {
									if(combination.getLevelOfCard() == Combination.LEVELS.THREE_KINDS) {
										return combination;
									} else if(combination.getLevelOfCard() == Combination.LEVELS.PAIRS) {
										return combination;
									}  else if(combination.getLevelOfCard() == Combination.LEVELS.PAIR) {
										return combination;
									}
								}
							}
						}
					}
                }
            }
        }
        return new Combination(Combination.LEVELS.NOTHING, hand.getHandCards().get(0));
    }
	
	public Hand getWinner(int countOfHands) {
		Hand winner = hands.get(0);
		int rank = Game.findCombination(winner).getLevelOfCard().getValue();

		for(int i = 0; i < countOfHands; i++ ) {
			if(Game.findCombination(hands.get(i)).getLevelOfCard().getValue() > rank) {
				winner = hands.get(i);
				rank = Game.findCombination(hands.get(i)).getLevelOfCard().getValue();
			}
		}

		return winner;
	}

	public static void main(String[] argc) {
		Hand hand1 = new Hand("Hand1");
		Hand hand2 = new Hand("Hand2");
        Hand hand3 = new Hand("Hand3");
        Hand hand4 = new Hand("Hand4");
		Game game = new Game();
		game.addHand(hand1);
		game.addHand(hand2);
        game.addHand(hand3);
        game.addHand(hand4);
        List<Hand> hands = new ArrayList<Hand>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);
        hands.add(hand4);

        Collections.sort(hands, HandComparator.getChain());


        for(int i = 0; i < hands.size(); i++) {
            System.out.println(hands.get(i).getName());
            System.out.println(hands.get(i).getCombination().getLevelOfCard());
            hands.get(i).getCombination().getRankCard().printCardDetails();
        }

		Hand winner = game.getWinner(4);

		System.out.println("The winner is " + winner.getName());
//		System.out.println("Winner's card " + Game.findCombination(winner).getLevelOfCard());
//		System.out.println("Looser card " + Game.findCombination(hand2).getLevelOfCard());

	}
}
